package com.xbongbong.saas.enums;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import lombok.Getter;

/**
 *
 * @author 魏荣杰
 * @date 2019/3/15 15:25
 * @since v1.0
 * @version v1.0
 */
@Getter
public enum WorkOrderStageRuleEnum {
    /**
     * 工单规则
     */
    DEFAULT(0,"default","enum.workOrderStageRule.name.default"),


    SEND_TO_CLOSE(1,"send_to_close","enum.workOrderStageRule.name.send_to_close"),


    SEND_TO_LESS(2,"send_to_less","enum.workOrderStageRule.name.send_to_less"),


    ROB(3,"rob","enum.workOrderStageRule.name.rob"),
    ;
    private Integer code;
    private String alias;
    private String name;

    WorkOrderStageRuleEnum(Integer code, String alias, String name) {
        this.code = code;
        this.alias = alias;
        this.name = name;
    }

    public static List<WorkOrderStageRuleEnum> getAllEnum() {
        return new ArrayList<>(Arrays.asList(values()));
    }

    public static JSONObject getJsonByCode(Integer code){
        for (WorkOrderStageRuleEnum cache : values()) {
            if (code.equals(cache.getCode())) {
                JSONObject json = new JSONObject();
                json.put("key",cache.getCode());
                json.put("value",cache.getName());
                return json;
            }
        }
        return null;
    }

    public static WorkOrderStageRuleEnum getByCode(Integer code) {
        for (WorkOrderStageRuleEnum cache : values()) {
            if (code.equals(cache.getCode())) {
                return cache;
            }
        }
        return null;
    }

    public static JSONObject getRule(){
        JSONObject rule = new JSONObject();
        JSONArray serviceRule = new JSONArray();
        serviceRule.add(getJsonByCode(1));
        serviceRule.add(getJsonByCode(2));
        serviceRule.add(getJsonByCode(3));
        rule.put("service",serviceRule);

        JSONArray departmentRule = new JSONArray();
        departmentRule.add(getJsonByCode(2));
        departmentRule.add(getJsonByCode(3));
        rule.put("department",departmentRule);

        rule.put("user",new JSONObject());

        return rule;
    }
    
    /**
     * 通过code获取派单规则的描述
     * 注：当code=0时，无需派单规则
     * @param code
     * @return java.lang.String
     * @author zcp
     * @date 2019/7/11 9:43
     * @since v1.0
     * @version v1.0
     */
    public static String getNameByCode(Integer code) {
        for (WorkOrderStageRuleEnum cache : values()) {
            if (code.equals(cache.getCode())) {
                return cache.getName();
            }
        }
        return "";
    }

    public String getName() {
        return I18nMessageUtil.getMessage(name);
    }
}
